﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using TencentWeiboSDK.Services;
using TencentWeiboSDK.Model;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using TencentWeiboSDK.Services.Util;
using System.Windows.Threading;

namespace QWeiBoN.ViewModule
{
    public class UserListModel : BaseViewModel
    {
        /// <summary>
        /// 实例化用户服务
        /// </summary>
        private UserService service = new UserService();

        public UserListModel() { }

        /// <summary>
        /// 根据用户编号列表得到用户信息
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="callback"></param>
        public void getUserFromUserIds(ICollection<string> userIds, Action<ICollection<User>> action)
        {
            // 一部刷新可以让用户有更好的操作体验
            new Thread(() =>
            {
                string param = String.Join(",", userIds.ToArray());
                service.Infos(new ServiceArgument() { Names = param }, (ucallback) =>
                {
                    ICollection<User> us = ucallback.Data as ICollection<User>;
                    if (null != action)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            action(us);
                        });
                    }
                });
            }).Start();
        }

        /// <summary>
        /// 用于根据关键字得到用户列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="action"></param>
        public void getUsersFromUserName(string keyword, Action<ICollection<User>> action)
        {
            // 异步刷新可以让用户有更好的操作体验
            new Thread(() =>
            {
                service.SearchUserInfo(new ServiceArgument() { Keyword = keyword, Pagesize = 15, Page = 1 }, (callback) =>
                {
                    ICollection<User> us = callback.Data as ICollection<User>;
                    if (null != action)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            action(us);
                        });
                    }
                });
            }).Start();
        }
    }
}
